package P121_ThreadPriority;

import org.junit.Test;


/**
 * 线程优先级只使用三个优先级
 *
 * @author xufy1
 */
public class ThreadPriority {

    // 1. 并不是严格按照线程优先级别来执行的
    // 2. 线程优先级推荐使用 MIN_PRIORITY NORM_PRIORITY MAX_PRIORITY
    // 3. Java无法保证优先级高的线程优先被执行，只能是有优先被执行的机会
    @Test
    public void test1() {
        for (int i = 0; i < 20; i++) {
            new TestThread().start(i % 10 + 1);
        }

    }

}

class TestThread implements Runnable {

    public void start(int _priority) {
        Thread t = new Thread(this);
        //设置线程优先级
//        t.setPriority(_priority>=10? Thread.MAX_PRIORITY:Thread.MIN_PRIORITY);
        t.setPriority(_priority);
        t.start();
    }

    @Override
    public void run() {
        //消耗cpu的计算
        for (int i = 0; i < 100; i++) {
            Math.hypot(Math.pow(924526789, i), Math.cos(i));
        }
        System.out.println("Priority: " + Thread.currentThread()
                );
    }
}
